package com.sweet.query.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 查询首页
 *
 * @author xiexiaofeng
 * @version 1.0     2017年09月09日  14点03分
 */
@Controller
@RequestMapping("")
public class IndexController
{

    @Resource(name = "queryComponent")
    private QueryComponent component;

    @RequestMapping("/")
    public String index()
    {
        return "index";
    }

    /**
     * 查询日志
     *
     * @param startTime 开始时间 格式：yyyy-MM-dd HH:mm:ss
     * @param endTime 结束时间 格式：yyyy-MM-dd HH:mm:ss
     * @param keyWord 查询关键字
     * @return 结果集
     * @throws ParseException 日期格式错误
     * @throws IOException 查询异常
     */
    @RequestMapping("/json/logs")
    @ResponseBody
    public String queryLogs(@RequestParam(name = "startTime") String startTime,
                                      @RequestParam(name = "endTime")   String endTime,
                                      @RequestParam(name = "condition") String condition,
                                      @RequestParam(name = "keyWord") String keyWord) throws ParseException, IOException
    {
        long start = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = component.getSearchService().search(format.parse(startTime).getTime(),
                format.parse(endTime).getTime(), condition, keyWord).toString();
        long end = System.currentTimeMillis();
        return "time: " + (end - start) + " ms <br>" + result;
    }
}
